let product=[
    {
        id:1,
        title:'论EF Core的自我修养',
        zaiyao:'论EF Core的自我修养',
        body:'论EF Core的自我修养',
        type:'.Net',
        author:'InCerry',
        time:'2022-04-06 08:47'
    },
    {
        id:2,
        title:'DDD之我见',
        zaiyao:'DDD之我见',
        body:'DDD之我见',
        type:'编程技术',
        author:'老胡来也',
        time:'2022-04-03 08:47'
    },
    {
        id:3,
        title:'论EF Core的自我修养',
        zaiyao:'论EF Core的自我修养',
        body:'论EF Core的自我修养',
        type:'.Net',
        author:'InCerry',
        time:'2022-04-06 08:47'
    },
]

let fn_product_list=(ctx,next)=>{
    ctx.body=product;
}
let fn_post=(ctx,next)=>{
    let obj=ctx.request.body;
    if(obj.length=1 && obj.name!==undefined){
        obj.id=product[product.length-1].id+1;
        product.push(obj);
        ctx.body={
            code:1000,
            data:obj,
            msg:'添加成功'
        }
    }else{
        ctx.body={
            code:404,
            data:null,
            msg:'添加不能为空'
        }
    }
}
let fn_put=(ctx,next)=>{

}
let fn_delete=(ctx,next)=>{
    let id=ctx.request.params.id;
    let arr=product.filter(item=>{
        return item.id!=id;
    })
    if(arr.length>0){
        product=arr;
        ctx.body={
            code:1000,
            data:'',
            msg:'删除成功'
        }
    }else{
        ctx.body={
            code:404,
            data:'',
            mag:'id不存在'
        }
    }
}

module.exports={
    'get /product':fn_product_list,
    'post /product':fn_post,
    'put /product':fn_put,
    'delete /product/:id':fn_delete
}